const ENV = __wxConfig.envVersion || 'develop'; // develop 表示开发版 trial 表示体验版 release 表示正式版
// 同时发送异步请求的次数
let ajaxTimes = 0;

//后台服务地址 http://192.168.200.44:8668 192.168.200.72
// pre地址 https://api-information-pre.uoko.com/
const serverUrl = {
    // trial:"https://api-information-pre.uoko.com",
    // release: "https://api-information.uoko.com",
    // develop: "http://192.168.200.72:8668",
    develop: "http://127.0.0.1:4523/mock/793601",
    // develop:"https://api-information.uoko.com",
    // develop: "https://api-information-pre.uoko.com",
    // develop:"http://172.16.2.94:8668"
};

export const request = (params) => {
    ajaxTimes++;
    const currentUrl = serverUrl[ENV];
    //定义公共的url
    const baseUrl = currentUrl;
    return new Promise((resolve, reject) => {
        wx.request({
            ...params,
            url: baseUrl + params.url,
            success: (result) => {
                if (result.data.resultCode === 10002) {
                    wx.showToast({
                        title: '操作失败！',
                        icon: 'none'
                    })
                    resolve(result)
                } else if (result.data.resultCode === 10001) {
                    wx.showToast({
                        title: '服务器异常！',
                        icon: 'none'
                    })
                    resolve(result)
                } else if (result.data.resultCode === 10003) {
                    wx.showToast({
                        title: '数据不存在！',
                        icon: 'none'
                    })
                    resolve(result)
                } else if (result.data.resultCode === 10010) {
                    wx.showToast({
                        title: result.data.resultMsg,
                        icon: 'none'
                    })
                    resolve(result)
                } else if (result.data.resultCode === 10014) {
                    resolve(result)
                } else if (result.data.resultCode === 10013) {
                    resolve(result)
                } else if (result.data.resultCode === 10012) {
                    resolve(result)
                } else if (result.data.resultCode === 10011) {
                    resolve(result)
                } else if (result.data.resultCode === 10000 || result.data.code === '000') {
                    resolve(result)
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '连接出错啦！',
                    icon: 'none'
                })
                reject(err);
            },
            complete: () => {
                ajaxTimes--;
                // 关闭加载中
                if (ajaxTimes === 0) {
                    return;
                }
            }
        });
    });
};

export const getViewInfo = (id) => {
    return new Promise((resolve, reject) => {
        try {
            wx.createSelectorQuery().select(id).boundingClientRect(res => 
                { resolve(res) 
            }).exec()
        } catch (err) {
            reject(err)
        }
    })
}  